package cn.itcast.ft.controller;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.itcast.config.VisibleThreadPoolTaskExecutor;
import cn.itcast.ft.dto.*;
import cn.itcast.ft.entity.FTToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class WHCController {
    private static List<FTToken> tokenList=new ArrayList<>();
    @Autowired
    private VisibleThreadPoolTaskExecutor executor;
    @RequestMapping("/chCreateMarket")
    public String wzhCreateMarket(@RequestParam(value = "num",defaultValue = "10000") Integer num,
                               @RequestParam(value = "salePrice",defaultValue = "29") String salePrice,
                               @RequestParam(value = "goodsId",defaultValue = "24") String goodsId) {
        if(!CollectionUtils.isEmpty(tokenList)) {
            for (int i = 0; i < num; i++) {
                try {
                    for (int z = 0; z < tokenList.size(); z++) {
                        Proxy proxy = null;
                        int finalZ = z;
//                    ThreadUtil.execAsync(() -> {
//                        confireMarketOrder(proxy,tokenList.get(finalZ),goodsId,salePrice);
//                    });
//                        executor.execute(()->{
//                            confireMarketOrder(proxy, tokenList.get(finalZ), goodsId, salePrice);
//                        });
                        confireMarketOrder(proxy, tokenList.get(finalZ), goodsId, salePrice);

                    }
                } catch (Exception e) {
                    log.error("#####异常####" + e.getMessage());
                }
            }
        }
        log.info("####FT执行完成#####");
        return null;
    }
    public void confireMarketOrder(Proxy proxy,FTToken token,String goodsId,String salePrice){
        try{
            createMarketOrder(proxy,token,goodsId,salePrice);
        }catch (Exception e){
            log.error("#####获取商品失败####"+e.getMessage()+"###"+token);
        }
    }
    public void createMarketOrder(Proxy proxy,FTToken token,String goodsId,String salePrice) throws InterruptedException {
        String response=null;
        WKJOrderDTO goodsDTO=null;
        try{
            JSONObject jsonObject=new JSONObject();
            jsonObject.putOnce("composite_id",goodsId);
            response = HttpRequest.post("https://duoliang.cc/api/v2/nft/composite/open")
                    .header("Authori-Zation",token.getToken())
                    .body(jsonObject.toString())
                    .execute().body();
            log.info("#####确认订单结果####"+response);
            goodsDTO= JSONUtil.toBean(response,WKJOrderDTO.class);
        }catch (Exception e){
            log.error("##获取商品数据异常###:"+e.getMessage()+"####返回数据：###"+response);
        }
        if(goodsDTO.getStatus()==200){
            MailUtil.send("1027060531@qq.com", "收到订单", "您有一个订单:"+token.getRefreshToken()+"，请赶紧支付！", false);
        }
    }
    @RequestMapping("/addUser")
    public String wzhCreateMarket(
                                  @RequestParam(value = "account") String account,
                                  @RequestParam(value = "wx") String wx,
                                  @RequestParam(value = "password") String password) {
        String response=null;
        WKUserDTO goodsDTO=null;
        try{
            JSONObject jsonObject=new JSONObject();
            jsonObject.putOnce("account",account);
            jsonObject.putOnce("password",password);
            response = HttpRequest.post("https://duoliang.cc/api/login")
                    .body(jsonObject.toString())
                    .execute().body();
            log.info("#####确认订单结果####"+response);
            goodsDTO= JSONUtil.toBean(response,WKUserDTO.class);
        }catch (Exception e){
            log.error("##获取商品数据异常###:"+e.getMessage()+"####返回数据：###"+response);
        }
        if(goodsDTO.getStatus()==200){
            FTToken token4=new FTToken();
            String token="Bearer "+goodsDTO.getData().getToken();
            token4.setToken(token);
            token4.setRefreshToken("微信名字:"+wx);
            tokenList.add(token4);
        }else{
            log.error("##账号密码错###:"+wx);
        }
        return null;
    }
}
